﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SiemensOPC;
using Opc.Ua;
using Opc.Ua.Client;
using VswService_V7.Config;
using VswService_V7.DataBase.Tables;
using VswService_V7.Utility;
using System.Data;

namespace VswService_V7.OPC.DB
{
    public class VS1AddressDbData : SubscriptionDbDataBase
    {
        public VS1AddressDbData(SimaticOPCClient opc, NodeConfig nodes) : base(opc, nodes)
        {

        }

        string _batchNo = "0";
        string _pipeNo = "0";

        string _Location = "VS1";

        /// <summary>
        /// 质量代码
        /// </summary>
		string _qualityCode = "0";

        /// <summary>
        /// 标记qc 是否发生改变，如果不改变不需要记录
        /// </summary>
        bool _qualityCodeChanged = false;

        public override void CreateSubscription()
        {
            opcServer.QuickAddMonitorItem(opcNodes.VS1Address.GrpoupName,
                opcNodes.VS1Address.PublishTime, 
                opcNodes.VS1Address.SampleTime,
                opcNodes.VS1Address.GetNodeAddress(), 
                opcNodes.VS1Address.GetNodeDisplayName(),
                SubscriptionHandle);


            opcServer.QuickAddMonitorItem(opcNodes.VS1PipeAddress.GrpoupName, 
                opcNodes.VS1PipeAddress.PublishTime, 
                opcNodes.VS1PipeAddress.SampleTime,
                opcNodes.VS1PipeAddress.GetNodeAddress(), 
                opcNodes.VS1PipeAddress.GetNodeDisplayName(),
                SubscriptionHandlePipe);
        }

        private void SubscriptionHandle(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null)
                return;
            if (monitoredItem.DisplayName == "VisionAsk")
            {
                bool ask;
                if (bool.TryParse(notification.Value.Value.ToString(), out ask))
                {
                    if (ask)
                    {
                        Console.WriteLine(_Location + " UPDATE REQ: " + notification.Value);
                        Task.Run(() => HandlePipe());
                    }
                    else
                    {
                        opcServer.WriteValue("false", opcNodes.VS1Address.VisionRsp);
                    }
                }
            }
        }

        private void SubscriptionHandlePipe(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null)
                return;

            if (notification.Value.Value.ToString().Equals("0"))
                return;

            switch (monitoredItem.DisplayName)
            {
                case "Batch_No":
                    {
                        _batchNo = notification.Value.Value.ToString();
                        break;
                    }
                case "Pipe_No":
                    {
                        _pipeNo = notification.Value.Value.ToString();
                        break;
                    }
                case "Quality_Code":
                    {
                        _qualityCode = notification.Value.Value.ToString();
                        _qualityCodeChanged = true;
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }





        private async Task HandlePipe()
        {

            await Task.Delay(1000);
            if (_batchNo.Equals("0") || _pipeNo.Equals("0") || !_qualityCodeChanged)
                return;
            //QC CODE 高低位交换
            Console.WriteLine(_Location + " handleqc start record");

            int pipeNum = int.Parse(_pipeNo.ToString());
            string pipeNo = _batchNo + pipeNum.ToString("000");

            try
            {
                PIPE_INFO.UpdatePipeInfoOnVis(pipeNo, _qualityCode, "0", _Location);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("HandlePipe", ex);
            }
            _qualityCodeChanged = false;
            opcServer.WriteValue("true", opcNodes.VS1Address.VisionRsp);
            Console.WriteLine(_Location + " qc changed: " + pipeNum);

        }
    }

}
